<?php
/**
 * User: 苏翔
 * Date: 2024/8/11 19:01
 */

namespace App\Service;

use App\Dao\HscoinBalanceDao;
use App\FundConf;
use App\HscoinConf;
use App\Model\HscoinBalanceModel;
use App\Model\HscoinBillModel;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Redis\Redis;

class HscoinBalanceService extends BaseService
{

    #[Inject]
    private Redis $redis;

    #[Inject]
    private HscoinBalanceDao $dao;

    public function __construct()
    {
        // $this->init();
    }

    function init(): bool
    {
        static $isInit = false;
        if (false === $isInit) {
            //  $isInit = $this->_init();
        }
        return $isInit;
    }


    /**
     * 金豆余额
     * @return float
     */
    function getBalance(): float
    {
        return $this->jsonSuccess(['balance' => $this->cacheService->getBalance()]);

        [$list, $status] = $this->cacheService->getCache();
        $rt = 0;
        foreach ($list as $item) {
            if (HscoinConf::STATS_TYPE_BALANCE == $item['cache_type']) {
                $rt = $item['cnt'];
                break;
            }
        }
        return $this->jsonSuccess(['balance' => $rt]);
    }

    function getMine($uid)
    {
        $info = $this->dao->getModel()->where('uid', $uid)->first(['cnt', 'income_cnt', Db::raw( 'if (last_date=curdate(),today_income_cnt,0) AS today_income_cnt')]);
        return $info?$info->toArray():[];
    }

    function listBalance($page, $limit, $uid)
    {
        $model = HscoinBalanceModel::query();
        if ($uid) {
            $model->where('uid', $uid);
        }

        $today = date('Y-m-d');

        $list = $model->orderBy('id', 'desc')->paginate($limit, ['*'], 'page', $page);
        foreach ($list as $k => $v) {
            $list[$k]['today_income_date'] = $today!=$v['last_date'] ? '0' : $v['today_income_date'];
        }
        return $list;
    }


}